//Array.prototype.filter():过滤重复的元素
//Array.prototype.findIndex():判断对象是否重复
var arr = [
    {id: 1, name: "a"}, {id: 2, name: "b"}, 
    {id: 1, name: "c"}, {id: 3, name: "d"},
    {id: 4, name: 'a'}
];
console.log(arr.findIndex(i=>i.id===2))//接受一个函数，返回数组下标
var uniqueArr = arr.filter((item, index) => arr.findIndex(i => i.id === item.id) === index);
console.log(uniqueArr);

var arr = [{id: 1, name: "a"}, {id: 2, name: "b"},{id: 1, name: "c"}, {id: 3, name: "d"}];
var uniqueArr = arr.reduce((acc, curr) => {
  if (!acc.find(item => item.id === curr.id)) {
    acc.push(curr);
  }
  return acc;
}, []);
console.log(uniqueArr);


var arr = [{id: 1, name: "a"}, {id: 2, name: "b"}, {id: 1, name: "c"}, {id: 3, name: "d"}];
var uniqueArr = [];
arr.forEach(item => {
  if (!uniqueArr.some(i => i.id === item.id)) {
    uniqueArr.push(item);
  }
});
console.log(uniqueArr);


var arr = [{id: 1, name: "a"}, {id: 2, name: "b"}, 
             {id: 1, name: "c"}, {id: 3, name: "d"}];
var uniqueArr = [...new Map(arr.map(item => [item.id, item])).values()];
console.log(uniqueArr);


var arr = [{id: 1, name: "a"}, {id: 2, name: "b"}, {id: 1, name: "c"}, {id: 3, name: "d"}];
var uniqueArr = arr.filter(function (item) {
    return !this[item.id] && (this[item.id] = true);
}, Object.create(null));
console.log(uniqueArr);

var arr = [{id: 1, name: "a"}, {id: 2, name: "b"}, {id: 1, name: "c"}, {id: 3, name: "d"}];
var unique = [];
for (const item of arr) {
  const isDuplicate = unique.find((obj) => obj.id === item.id);
  if (!isDuplicate) {
    unique.push(item);
  }
}
console.log(unique);




//根据多个属性惊醒判断
var arr = [
  {id: 1, name: "a"}, {id: 2, name: "b"}, {id: 1, name: "c"}, {id: 3, name: "d"},
];
var uniqueArr = arr.filter((item, index) => arr.findIndex(i => i.id === item.id&&i.name==item.name) === index);
console.log(uniqueArr);

var arr = [{id: 1, name: "a"}, {id: 2, name: "b"}, {id: 1, name: "c"}, {id: 3, name: "d"}];
var unique = [];
for (var item of arr) {
  var isDuplicate = unique.find((obj) =>obj.id === item.id && obj.name ==item.name);
  if (!isDuplicate) {
    unique.push(item);
  }
}
console.log(unique)

const generateUUID = (a) => a?(a ^ ((Math.random() * 16) >> (a / 4))).toString(16):([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g,generateUUID);
console.log(generateUUID())